package com.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.server.pojo.SearchRequest;
import com.server.pojo.Users;
import com.server.service.UsersService;
import com.server.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * <p>
 * 平台用户信息表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 09:29:27
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;
 @GetMapping("getAllUsers/{page}/{limit}")
    public ResponseMsg getAllUsers(@PathVariable Integer page,
                                   @PathVariable Integer limit){
        IPage<Users> p= new Page<>(page, limit);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"分页查询用户数据",usersService.page(p));
    }

    /**
     * 强制用户退出
     * @param phone 电话号
     * @return 退出结果
     */
    @GetMapping("deleteUser/{phone}")
    public ResponseMsg deleteUser(@PathVariable Integer phone){
     Boolean result = usersService.delUserToken(phone);
     if( result){
         return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"退出成功");
     }else{
         return ResponseMsg.getInstance(ResponseMsg.ERROR,"退出失败");
     }
    }

    /**
     * 获取图文中的数据
     * @return 获取结果
     */
    @GetMapping("getMessage")
    public ResponseMsg getMessage(){
        Integer[] users = usersService.getMessage();
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"获取消息成功",users);
    }

    /**
     * 用户名模糊查询
     * @param page 页码
     * @param limit 条数
     * @param key 关键词
     * @return 返回结果
     */
    @PostMapping("searchUser")
    public ResponseMsg getLikeUser(@RequestBody SearchRequest s){
        IPage<Users> p= new Page<>(s.getPage(), s.getLimit());
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.like("username",s.getKey());
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"获取用户成功",usersService.UserPage(p,wrapper));
    }
}
